package cn.sciento.transfer.infra.datasource.ddl.utils;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.sciento.core.util.FieldNameUtils;

public class ResultSetUtils {
  public static List<Map<String, Object>> changeResultSetToList(ResultSet resultSet, Boolean convertCamel) throws SQLException {
    List<Map<String, Object>> list = new ArrayList<>();
    ResultSetMetaData md = resultSet.getMetaData();
    int columnCount = md.getColumnCount();
    while (resultSet.next()) {
      Map<String, Object> rowData = new HashMap<>();
      for (int i = 1; i <= columnCount; i++) {
        String columnName = md.getColumnName(i);
        if (convertCamel.booleanValue())
          columnName = FieldNameUtils.underline2Camel(md.getColumnName(i), true); 
        rowData.put(columnName, resultSet.getObject(i));
      } 
      list.add(rowData);
    } 
    return list;
  }
}
